package com.wpml.netty.rpc.server;

import com.wpml.netty.rpc.Dispatcher;
import com.wpml.netty.rpc.service.Car;
import com.wpml.netty.rpc.service.CarServiceImpl;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;

import java.net.InetSocketAddress;

public class RPCServer {

    static String transType = "rpc";

    public static void startServer() {
        NioEventLoopGroup boss = new NioEventLoopGroup(1);
        NioEventLoopGroup worker = new NioEventLoopGroup(1);

        ServerBootstrap server = new ServerBootstrap();
        ChannelFuture future = server.group(boss, worker)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        System.out.println("server accept port : " + ch.remoteAddress().getPort());
                        ch.pipeline()
                                .addLast(new ServerDecode())
                                .addLast(new ServerRequestHandler());
                    }
                }).bind(new InetSocketAddress("localhost", 9090));

        try {
            System.out.println("server start ...");
            future.sync().channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {

        transType = "http";

        Dispatcher dis = Dispatcher.getInstance();
        dis.register(Car.class.getName(), new CarServiceImpl());

        NioEventLoopGroup boss = new NioEventLoopGroup(1);
        NioEventLoopGroup worker = new NioEventLoopGroup(5);

        ServerBootstrap server = new ServerBootstrap();
        ChannelFuture future = server.group(boss, worker)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        System.out.println(Thread.currentThread().getName() + " server accept port : " + ch.remoteAddress().getPort());
                        ChannelPipeline p = ch.pipeline();
                        if (transType.equals("rpc")) {
                            p.addLast(new ServerDecode());
                            p.addLast(new ServerRequestHandler());
                        } else {
                            // http协议
                            p.addLast(new HttpServerCodec());
                            p.addLast(new HttpObjectAggregator(1024*512));
                            p.addLast(new HttpRequestHandler());
                        }
                    }
                }).bind(new InetSocketAddress("localhost", 9090));

        try {
            System.out.println("server start ...");
            future.sync().channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
